/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exterminadordeburbujas;

import java.awt.event.ActionEvent;
import javax.swing.JButton;

/**
 *
 * @author Jose Gabriel
 */
public class Burbuja extends JButton {
    /*
     * Atributos: contenido y maximo
     * Metodos: transferir y recibir
     */
    private int gas;
    private final int MAX_GAS;
    private final int DIRECCIONES = 4;
    private boolean flag=false;

    public Burbuja(int MAX_GAS, int gas) {
        this.gas = gas;
        this.MAX_GAS = MAX_GAS;
        if(this.gas==0){
            this.setText("");
        }else{
            this.setText(this.gas+"");
        }
    }
    
    public void recibir (int gasRecibido){
        if(this.gas < this.MAX_GAS-1){
            this.gas+=gasRecibido;
            this.setText(this.gas+"");
        }else{
            this.setText("X");
            this.gas=this.MAX_GAS;
        }
    }
    
    public void transferir (int x, int y){
        ExterminadorDeBurbujas.jFrame.transferirDer(this.gas/DIRECCIONES, x+1, y);
        ExterminadorDeBurbujas.jFrame.transferirIzq(this.gas/DIRECCIONES, x-1, y);
        ExterminadorDeBurbujas.jFrame.transferirArr(this.gas/DIRECCIONES, x, y-1);
        ExterminadorDeBurbujas.jFrame.transferirAba(this.gas/DIRECCIONES, x, y+1);
        if(!this.flag){
            
            this.flag=true;
            transferir(x,y);
        }
        
    }
    
    public boolean isFull(){
        if(this.gas == this.MAX_GAS){
            return true;
        }
        return false;
    }
    
    public boolean isEmpty(){
        if(this.gas == 0){
            return true;
        }
        return false;
    }
    
    public void iniciar(){
        this.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                evento(evt);
            }
        });
    }
    
    public void evento(ActionEvent event){
        
        int x= this.getActionCommand().charAt(0)-48;
        int y= this.getActionCommand().charAt(1)-48;
        
        recibir(1);
        if(!this.flag && isFull()){ 
            this.flag=true;
            transferir(x,y);
        }
        
    }

}
